package com.example.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import com.example.domain.Chapter;
import com.example.domain.Novel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@WebServlet("/NovelContentServlet")
public class NovelContentServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
    {
        response.setContentType("text/html;charset=utf-8");

        HttpSession session = request.getSession();
        List<Chapter> chapterList = (List<Chapter>)session.getAttribute("chapterList");
        Integer chapter_id = Integer.parseInt(request.getParameter("chapter_id"));
        for(Chapter chapter:chapterList){
            if(chapter.getChapter_id()==chapter_id){
                String content = chapter.getContent().replaceAll("\\s{1,}", "<br>").trim();
                chapter.setContent(content);
                request.setAttribute("chapter", chapter);
                break;
            }
        }

        request.getRequestDispatcher("novelContent.jsp").forward(request, response);
    }
}
